#!/usr/bin/python 
import urllib
import re
import sys

port = 80
domain = sys.argv[1]
if(sys.argv[2]):
  port = sys.argv[2]

def fetchTrain(base):
  url = base + "train"
  for i in range(5):
    print "i = ", i
    count = 0
    while True:
      if(count > 5):
        break
      try:
        tmp = url
        url = base + "train?page=%d" % (i)
        data = urllib.urlopen(url).read()
        print data
        url = tmp 
        try:
          data = int(data)
        except:
          data = 0

        if(data >= 15):
          break
      except:
        continue
      count = count + 1

def fetchRoad(base):
  url = base + "road"
  for i in range(5):
    print "i = ", i
    count = 0
    while True:
      if(count > 5):
        break
      try:
        tmp = url
        url = base + "road?page=%d" % (i)
        data = urllib.urlopen(url).read()
        print data
        url = tmp
        try:
          data = int(data)
        except:
          data = 0
        if(data >= 15):
          break
      except:
        pass
      count = count + 1
def fetchAlarmEx(base):
  url = base + "alarmEx"
  i = 0
  while True:
    try:
      print url
      data = urllib.urlopen(url).read()
      i = i + 1
      print data
      try:
        data = int(data)
      except:
        data = 0
      if(data >= 30):
        break
      if( i > 5):
        break;
    except:
      continue
 
def fetchAlarm(base):
  url = base + "alarm"
  i = 0
  while True:
    try:
      print url
      data = urllib.urlopen(url).read()
      i = i + 1
      print data
      if(data != "0"):
        break
      if( i > 5):
        break;
    except:
      continue
 
def fetchView(base):
  url = base + "view"
  count = 0
  while(True):
    try:
      count = count + 1
      print url
      data = urllib.urlopen(base + "count").read()
      print data
      if(data != '0'):
        data = urllib.urlopen(url).read()
      else:
        break
      if count > 500:
        break;
    except:
      exit()
def fetchUpdateAlarm(base):
  url = base + "updateAlarm"
  road = 0
  alarm = 0
  data = urllib.urlopen(url).read()
  print "alarm = ", data

def fetchUpdateRoad(base):
  url = base + "updateRoad"
  for i in range(5):
    print "i = ", i
    count = 0
    while True:
      if(count > 5):
        break
      try:
        tmp = url
        url = base + "updateRoad?page=%d" % (i)
        data = urllib.urlopen(url).read()
        print "val = ", data
        url = tmp 
        if(data == "1"):
          break
      except:
        continue
      count = count + 1
  print data

def fetchUpdateTrain(base):
  url = base + "updateTrain"
  for i in range(5):
    print "i = ", i
    count = 0
    while True:
      try:
        tmp = url
        url = base + "updateTrain?page=%d" % (i)
        print url
        data = urllib.urlopen(url).read()
        count = count + 1
        if count > 5:
          break;
        print data
        url = tmp 
        if(data == "1"):
          break
      except:
        continue
  print data

def fetchUpdate(base):
  url = base + "update"
  print url
  road = 0
  alarm = 0
  while(True):
    data = urllib.urlopen(url).read()
    if(data == "3"):
      road = road + 1
      fetchRoad(base)
      fetchView(base)

    if(data == "2"):
      alarm = alarm + 1
      fetchAlarmEx(base)
      fetchView(base)
    if(data == "1"):
      break
    if(alarm > 5):
      break
    if(road > 5):
      break


def Fetch(domain, port):
  base = "http://" + domain + ":" + port + "/"
  fetchRoad(base)
  fetchTrain(base)
  fetchAlarmEx(base)
  fetchView(base)
  fetchUpdateAlarm(base)
  fetchUpdateRoad(base)
  fetchUpdateTrain(base)
  
Fetch(domain, port)

